package com.firebugsoft.metadata.dubbo.impl.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.firebugsoft.metadata.dubbo.api.IDictionaryService;
import com.firebugsoft.metadata.dubbo.api.to.DictionaryTO;
import com.firebugsoft.metadata.dubbo.impl.dao.DictionaryDAO;
import com.firebugsoft.metadata.dubbo.impl.po.DictionaryPO;

@Service
public class DictionaryService implements IDictionaryService {
    @Resource
    private DictionaryDAO dictionaryDAO;

    @Override
    public DictionaryTO findByCode(String code) {
        DictionaryPO po = dictionaryDAO.findByCode(code);
        return po2to(po, new DictionaryTO());
    }

    @Override
    public List<DictionaryTO> findByPcode(String pcode) {
        List<DictionaryPO> pos = dictionaryDAO.findByPcode(pcode);
        List<DictionaryTO> tos = new ArrayList<>(pos.size());
        for (DictionaryPO po : pos) {
            tos.add(po2to(po, new DictionaryTO()));
        }
        return tos;
    }

    private DictionaryTO po2to(DictionaryPO po, DictionaryTO to) {
        to.setCode(po.getCode());
        to.setPcode(po.getPcode());
        to.setName(po.getName());
        to.setSort(po.getSort());
        return to;
    }
}
